package com.xxja.order.cart.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xxja.order.cart.car.pojo.Cart;
import com.xxja.order.cart.car.service.CartService;
import com.xxja.order.utils.PageRequest;
import com.xxja.order.utils.ResponseBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("cart")
public class CartController {
    @Autowired
    private CartService cartService;

    /**
     * 添加购物车
     * @param cart body数据
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> addCart(@RequestBody Cart cart) {
        this.cartService.addCart(cart);
        return ResponseBuilder.ok();
    }

    /**
     * 查询购物车
     * @param pageRequest
     * @return
     */
    @GetMapping
    public ResponseEntity<List<Cart>> queryCartList(PageRequest pageRequest) {
        IPage<Cart> carts = this.cartService.queryCartList(pageRequest);
        if (carts == null) {
            return ResponseBuilder.allStatus(404);
        }
        return ResponseBuilder.responseIpage(carts, 200);
    }

    /**
     * 更改商品数量
     * @param cart
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> updateNum(@RequestBody Cart cart) {
        this.cartService.updateCarts(cart);
        return ResponseBuilder.allStatus(204);
    }

    /**
     * 删除购物车中商品
     * @param goodId
     * @return
     */
    @DeleteMapping("{goodId}")
    public ResponseEntity<Void> deleteCart(@PathVariable String goodId) {
        this.cartService.deleteCart(goodId);
        return ResponseBuilder.allStatus(204);

    }
}



